import { sendSock } from '@/plugins/request/websocket';
export default {
    namespaced: true,
    state: {
        tip: '', // 提示
        error: '', // 错误信息
        token: localStorage.getItem('wsInfo') ? JSON.parse(localStorage.getItem('wsInfo')).SetToken : '', // token
        dialog: '', // dialog页面
        dialogLock: 0, // dialog锁, 0没有锁
        user: localStorage.getItem('wsInfo') ? JSON.parse(localStorage.getItem('wsInfo')).SetUser : {}, // 当前登陆的用户信息
        login: false, // 登陆状态
        relogin: false, // 自动重连
        loginInfo: {}, // 登陆信息，用语断线重连
        // ===============================================================================
        wechats: [], // 当前用户绑定的微信,不是从数据库读取的
        wechatsMap: {}, // 微信id和微信详情对应关系{id:{wx}}，从数据库读取的
        friends: {}, // 从数据库读取所有的好友
        labels: {}, // 微信id-labels
        members: {} // 陌生人
    },
    getters: {
        error: (state) => state.error,
        tip: (state) => state.tip,
        user: (state) => state.user,
        // 查询nedb用到的用户id
        userId: (state) => JSON.stringify(state.user) === '{}' ? '' : state.user.SupplierId + state.user.UnionId,
        token: (state) => state.token,
        login: (state) => state.login,
        relogin: (state) => state.relogin,
        loginInfo: (state) => state.loginInfo,
        dialog: (state) => state.dialog,
        dialogLock: (state) => state.dialogLock,

        // ===============================================================================
        // 用户绑定的微信
        wechats: (state) => state.wechats,
        // 当前登陆的微信
        wechatsLogin: (state) => {
            let wechatsLogin = [];
            for (const wechat of state.wechats) {
                if (wechat.IsLogined) {
                    wechatsLogin.push(wechat);
                }
            }
            return wechatsLogin;
        },
        // wechatid - detail
        wechatsMap: (state) => state.wechatsMap,
        // 所有微信的所有好友
        friends: (state) => state.friends,
        // 指定微信的好友的friendId - avatar - nickname
        friendsMap: (state) => {
            let fm = {};
            let friends = state.friends;
            for (const key in friends) {
                let friendsArry = {};
                if (friends.hasOwnProperty(key)) {
                    const friendsInfo = friends[key];
                    for (const friend of friendsInfo) {
                        friendsArry[friend.FriendId] = friend;
                    }
                }
                fm[key] = friendsArry;
            }
            // console.log('friendsMap')
            // console.log(fm)
            return fm;
        },
        labels: (state) => state.labels,
        members: (state) => state.members
    },
    mutations: {
        /**
         * @description 设置下拉菜单数据
         * @param {Object} state vuex state
         * @param {Array} menu menu
         */
        SET_ERROR: (state, error) => {
            state.error = error;
        },
        SET_TIP: (state, tip) => {
            state.tip = tip;
        },
        SET_USER: (state, user) => {
            state.user = user;
        },
        SET_TOKEN: (state, token) => {
            state.token = token;
        },
        SET_LOGIN: (state, flag) => {
            state.login = flag;
        },
        SET_RELOGIN: (state, flag) => {
            state.relogin = flag;
        },
        SET_LOGIN_INFO: (state, info) => {
            state.loginInfo = info;
        },
        SET_DIALOG: (state, dialog) => {
            state.dialog = dialog;
        },
        SET_DIALOG_LOCK: (state, lock) => {
            state.dialogLock = lock;
        },
        // ===============================================================================
        SET_WECHATS: (state, wechats) => {
            state.wechats = wechats;
        },
        // SET_WECHATS_LOGIN: (state, wechatsLogin) => {
        //     // let i = 0
        //     for (const wechat of state.wechats) {
        //         wechat.IsLogined = false
        //         if (wechatsLogin.indexOf(wechat.WeChatId) >= 0) {
        //             wechat.IsLogined = true
        //         }
        //         // Vue.set(state.wechats, i, wechat)
        //         i++
        //     }
        // },
        SET_WECHATS_MAP: (state, wm) => {
            state.wechatsMap = wm;
        },
        SET_FRIENDS: (state, friendsList) => {
            state.friends = friendsList;
        },
        SET_MEMBERS: (state, membersList) => {
            state.members = membersList;
        },
        SET_LABELS: (state, labels) => {
            state.labels = labels;
        }
    },
    actions: {
        // 设置错误信息
        SetError ({ commit }, error) {
            commit('SET_ERROR', error);
        },
        // 设置提示
        SetTip ({ commit }, tip) {
            commit('SET_TIP', tip);
        },
        // 设置当前用户
        SetUser ({ commit }, user) {
            commit('SET_USER', user);
        },
        // 设置token
        SetToken ({ commit }, token) {
            commit('SET_TOKEN', token);
        },
        // 登陆状态
        SetLogin ({ commit }, flag) {
            commit('SET_LOGIN', flag);
        },
        SetRelogin ({ commit }, flag) {
            commit('SET_RELOGIN', flag);
        },
        SetLoginInfo ({ commit }, info) {
            commit('SET_LOGIN_INFO', info);
        },
        // 设置弹框
        SetDialog ({ commit, getters }, dialog) {
            let flag = getters.dialogLock;
            switch (flag) {
            case 0:
                console.log(`没有上锁，可以跳转到${dialog}`);
                commit('SET_DIALOG', dialog);
                break;
            // 预加载页面
            case 1:
                console.log(`信息加载中，不可以跳转到${dialog}`);
                break;
            // 选择微信登陆
            case 2:
                if (dialog !== '') {
                    commit('SET_DIALOG', dialog);
                }
                break;
            // 群管理相关操作
            case 3:
                if (dialog !== '') {
                    commit('SET_DIALOG', dialog);
                }
                break;
            // 备注相关操作
            case 4:
                if (dialog !== '') {
                    commit('SET_DIALOG', dialog);
                }
                break;
            // 获取单个好友详情
            case 5:
                if (dialog !== '') {
                    commit('SET_DIALOG', dialog);
                }
                break;
            // 退出登陆
            case 6:
                if (dialog !== '') {
                    commit('SET_DIALOG', dialog);
                }
                break;
            // 删除好友
            case 7:
                if (dialog !== '') {
                    commit('SET_DIALOG', dialog);
                }
                break;
            // 发名片
            case 8:
                if (dialog !== '') {
                    commit('SET_DIALOG', dialog);
                }
                break;

            default:
                break;
            }
        },
        // 设置dialog锁
        SetDialogLock ({ commit }, lock) {
            console.log('设置dialog锁', lock);
            commit('SET_DIALOG_LOCK', lock);
        },

        // ===============================================================================
        // 设置当前用户绑定的微信
        SetWechats ({ commit }, wechats) {
            commit('SET_WECHATS', wechats);
        },
        // 设置当前用户绑定的微信的登陆状态
        // SetWechatsLogin ({ commit }, wechatsLogin) {
        //     commit('SET_WECHATS_LOGIN', wechatsLogin)
        // }
        // 拉取微信列表信息
        getWechats ({ commit }, user) {
            let msg = {
                Id: 3050, // 自定义，可以不传
                MsgType: 'GetWeChatsReq',
                Content: {
                    AccountType: user.AccountType,
                    UnionId: user.UnionId
                }
            };
            sendSock(msg);
        },
        // 获取微信好友列表
        // WeChatLoginNotice
        wechatesLogin ({ commit }, wechatsInfo) {
            let content = {
                SupplierId: this.state.websocket.account.user.SupplierId,
                UnionId: this.state.websocket.account.user.UnionId,
                AccountType: this.state.websocket.account.user.AccountType,
                WeChats: wechatsInfo
            };
            let msg = {
                Id: 3055,
                AccessToken: this.state.websocket.account.token,
                MsgType: 'WeChatLoginNotice',
                Content: content
            };
            sendSock(msg);
        }
    }
};
